package com.dongway.netcondition.socketToBox;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;

import com.dongway.netcondition.model.DevInfo;

public class SetNetwork {

    public static void setNetwork(DevInfo devInfo) {

        Integer dev_id = devInfo.getId();

        Socket socket = SocketContent.getInstance().getSocket(dev_id);
        char[] IpA = devInfo.getNetCardAIp().toCharArray();
        char[] IpaSubnet = devInfo.getNetCardASubIp().toCharArray();
        String[] IpaGateway = devInfo.getNetCardAGatewayIp().split("\\.");
        String[] IpaMac = devInfo.getNetCardAMacAddress().split(":");

        char[] IpB = devInfo.getNetCardBIp().toCharArray();
        char[] IpbSubnet = devInfo.getNetCardBSubIp().toCharArray();
        String[] IpbGateway = devInfo.getNetCardBGatewayIp().split("\\.");
        String[] IpbMac = devInfo.getNetCardBMacAddress().split(":");

        char[] Ip3g = devInfo.getNetCard3gIp().toCharArray();
        char[] Ip3gSubnet = devInfo.getNetCard3gSubIp().toCharArray();
        String[] Ip3gGateway = devInfo.getNetCard3gGatewayIp().split("\\.");
        String[] Ip3gMac = devInfo.getNetCard3gMacAddress().split(":");

        byte[] b3 = new byte[156];

        // char[] IpA = iplisterner.getIpA().toCharArray();
        // char[] IpaSubnet = iplisterner.getIpaSubnet().toCharArray();
        // String[] IpaGateway = iplisterner.getIpaGateway().split(".");
        // String[] IpaMac = iplisterner.getIpaMac().split(":");
        //
        // String IpB = iplisterner.getIpA();
        // char[] IpbSubnet = iplisterner.getIpaSubnet().toCharArray();
        // String[] IpbGateway = iplisterner.getIpaGateway().split(".");
        // String[] IpbMac = iplisterner.getIpaMac().split(":");

        // char[] IpB="172.21.6.231".toCharArray();
        // char[] IpbSubnet ="255.255.255.0".toCharArray();
        // String[] IpbGateway = { "172", "21", "6", "254"};
        // String[] IpbMac = { "02", "0e", "0b", "41", "d0", "05" };
        //
        //
        // char[] IpA="172.21.6.235".toCharArray();
        // char[] IpaSubnet ="255.255.255.0".toCharArray();
        // String[] IpaGateway = { "172", "21", "6", "254" };
        // String[] IpaMac = { "02", "81", "04", "02", "03", "12" };

        // char[] Ip3g="0.0.0.0".toCharArray();
        // char[] Ip3gSubnet ="0.0.0.0".toCharArray();
        // String[] Ip3gGateway = { "0", "0", "0", "0" };
        // String[] Ip3gMac = { "00", "00", "00", "00", "00", "00" };

        String ID = String.valueOf(dev_id);

        String hex = "";
        int ox16;
        // 发送报文指令头
        String[] s0 =
                { "61", "31", "30", "2c", "88", "87", "02", "06", "92", "00", "00", "00", "00",
                 "00", "00", "00" };

        for (int i = 0; i < 16; i++) {
            hex = s0[i];
            if (i == 10)
                hex = ID;
            hex = (hex.length() == 2 ? hex : ("0" + hex));
            b3[i] = HexStringToBytes.hexStringToBytes(hex)[0];
        }

        // 网卡B的IP地址
        for (int i = 16; i < 16 + IpB.length; i++) {
            ox16 = IpB[i - 16];
            hex = Integer.toHexString(ox16);
            hex = (hex.length() == 2 ? hex : ("0" + hex));
            b3[i] = HexStringToBytes.hexStringToBytes(hex)[0];
        }

        // 网卡b 的默认网关
        for (int i = 32; i < 32 + IpbSubnet.length; i++) {
            ox16 = IpbSubnet[i - 32];
            hex = Integer.toHexString(ox16);
            hex = (hex.length() == 2 ? hex : ("0" + hex));
            b3[i] = HexStringToBytes.hexStringToBytes(hex)[0];
        }

        // 网卡B的默认网关以及Mac地址
        for (int i = 48; i < 58; i++) {
            if (i < 52) {
                ox16 = Integer.parseInt(IpbGateway[i - 48]);
                hex = Integer.toHexString(ox16);
                hex = (hex.length() == 2 ? hex : ("0" + hex));
                b3[i] = HexStringToBytes.hexStringToBytes(hex)[0];
            } else {
                hex = IpbMac[i - 52];
                hex = (hex.length() == 2 ? hex : ("0" + hex));
                b3[i] = HexStringToBytes.hexStringToBytes(hex)[0];

            }
        }

        // 网卡A的IP地址
        for (int i = 64; i < 64 + IpA.length; i++) {
            ox16 = IpA[i - 64];
            hex = Integer.toHexString(ox16);
            hex = (hex.length() == 2 ? hex : ("0" + hex));
            b3[i] = HexStringToBytes.hexStringToBytes(hex)[0];
        }

        // 网卡A的默认网关
        for (int i = 80; i < 80 + IpaSubnet.length; i++) {
            ox16 = IpaSubnet[i - 80];
            hex = Integer.toHexString(ox16);
            hex = (hex.length() == 2 ? hex : ("0" + hex));
            b3[i] = HexStringToBytes.hexStringToBytes(hex)[0];
        }

        // 网卡A默认网关以及mac地址ַ
        for (int i = 96; i < 106; i++) {
            if (i < 100) {
                ox16 = Integer.parseInt(IpaGateway[i - 96]);
                hex = Integer.toHexString(ox16);
                hex = (hex.length() == 2 ? hex : ("0" + hex));
                b3[i] = HexStringToBytes.hexStringToBytes(hex)[0];
            } else {

                hex = IpaMac[i - 100];
                hex = (hex.length() == 2 ? hex : ("0" + hex));
                b3[i] = HexStringToBytes.hexStringToBytes(hex)[0];

            }
        }

        // 无线3G的IP的地址
        for (int i = 112; i < 112 + Ip3g.length; i++) {
            ox16 = Ip3g[i - 112];
            hex = Integer.toHexString(ox16);
            hex = (hex.length() == 2 ? hex : ("0" + hex));
            b3[i] = HexStringToBytes.hexStringToBytes(hex)[0];
        }
        // 无线3G的默认网关
        for (int i = 128; i < 128 + Ip3gSubnet.length; i++) {
            ox16 = Ip3gSubnet[i - 128];
            hex = Integer.toHexString(ox16);
            hex = (hex.length() == 2 ? hex : ("0" + hex));
            b3[i] = HexStringToBytes.hexStringToBytes(hex)[0];
        }

        // 无线3G的默认网关以及Mac地址ַ
        for (int i = 144; i < 154; i++) {
            if (i < 148) {
                ox16 = Integer.parseInt(Ip3gGateway[i - 144]);
                hex = Integer.toHexString(ox16);//
                hex = (hex.length() == 2 ? hex : ("0" + hex));
                b3[i] = HexStringToBytes.hexStringToBytes(hex)[0];
            } else {

                hex = Ip3gMac[i - 148];
                hex = (hex.length() == 2 ? hex : ("0" + hex));
                b3[i] = HexStringToBytes.hexStringToBytes(hex)[0];

            }
        }

        try {
            OutputStream out = socket.getOutputStream();
            out.write(b3);
            out.close();

        } catch (IOException e) {
            System.out.println("设置失败！");
        }
    }

}
